package com.lungle.warmdiary.app.ui.horizontal;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.utils.StringUtils;

import java.util.List;

/**
 * Created by zhangxueyuan on 16/10/4.
 */
public class HorizontalScrollViewAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> mDatas;

    public HorizontalScrollViewAdapter(Context context, List<String> mDatas) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = mDatas;
    }

    public int getCount() {
        return mDatas.size();
    }

    public Object getItem(int position) {
        return mDatas.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.item_campaign_screenshot, parent, false);
            viewHolder.mImg = (ImageView) convertView
                    .findViewById(R.id.iv_img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
                .imageScaleType(ImageScaleType.EXACTLY)
                .showImageOnLoading(R.drawable.big_imgplaceholder)
                .showImageForEmptyUri(R.drawable.big_imgplaceholder)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        if (!TextUtils.isEmpty(mDatas.get(position)))
            ImageLoader.getInstance().displayImage(StringUtils.getPreviewImageUrl(mDatas.get(position)), viewHolder.mImg, displayOptions);
        return convertView;
    }

    private class ViewHolder {
        ImageView mImg;
    }

}
